package com.esir.projet.keriosity.controle;

import java.io.IOException;
import java.rmi.Naming;
import java.rmi.NotBoundException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kercar.device.RemoteDevice;

public class ManualControl extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4097446279692979273L;
	
	public static final String 	RMI_IP = "127.0.0.1",
								RMI_NAME = "MasterChief",
								UP = "z",
								DOWN = "s",
								LEFT = "q",
								RIGHT = "d",
								STOP = "e";
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String ordre = req.getParameter("ordre");
		RemoteDevice remoteDevice;
		try {
			remoteDevice = (RemoteDevice) Naming.lookup("rmi://" + RMI_IP + "/" + RMI_NAME);
			
			if(ordre.equals(UP)) {
				remoteDevice.moveForward(20);
			} else if(ordre.equals(LEFT)) {
				remoteDevice.turn(1);
			} else if(ordre.equals(RIGHT)) {
				remoteDevice.turn(-1);
			} else if(ordre.equals(DOWN)) {
				remoteDevice.moveBackward(20);
			} else if(ordre.equals(STOP)) {
				remoteDevice.stopMoving();
			}
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
	}
	
}
